#include "IdMapTest.h"
#include "CBL_IdMap.h"

#include <iostream>

struct CTestOp : public Cb::CIdMap::IIdOp
{
	uint32 m_Count;
	
	CTestOp() : m_Count(0) {}
	
	virtual void operator () (Cb::IdType _Id)
	{
		++m_Count;
	}
};


BEGIN_TEST_CASE_IMPLEMENTATION(CIdMapTest)
	
	Cb::CIdMap Test(10);
	const char* pTestName = "test";
	const char* pTestName2 = "test2";
	const char* pTestName3 = "test3";
	
	Cb::IdType Id1 = Test.GetId(pTestName);		// should be invalidid
	Cb::IdType Id2 = Test.RegisterId(pTestName);	// should be 0
	Cb::IdType Id3 = Test.GetId(pTestName);		// should be same as id2
	Cb::IdType Id4 = Test.RegisterId(pTestName);	// should be same as id3
	
	Cb::IdType Id5 = Test.RegisterId(pTestName2);	// should be 1
	Cb::IdType Id6 = Test.GetId(pTestName2);		// should be same as id5
	
	Test.Lock();
	Cb::IdType Id7 = Test.RegisterId(pTestName3);	// should be InvalidId
	
	std::string TestName = Test.GetName(Id2);		// Should be "test"
	std::string TestName2 = Test.GetName(Id5);		// Should be "test2"
	std::string InvalidName = Test.GetName(Id1);	// Should be "InvalidId"
	std::string InvalidName2 = Test.GetName(15);	// Should be "InvalidId"
	
	CTestOp IdCounter;
	Test.ForEachId(IdCounter); // Should be 2
	
	CHECK(IdCounter.m_Count == 2,		"> ForEach count test failed.")
	CHECK(Id1 == Cb::InvalidId,	"> Invalid Id test failed.")
	CHECK(Id2 == 0,						"> First Id=0 test failed.")
	CHECK((Id2 == Id3) && (Id3 == Id4), "> (Re)registered Id tests failed.")
	CHECK(Id5 == 1,						"> Second Id=1 test failed.")
	CHECK(Id6 == Id5,					"> Get second Id test failed.")
	CHECK(Id7 == Cb::InvalidId,	"> Lock test failed.")
	CHECK(TestName == pTestName,		"> Name test failed.")
	CHECK(TestName2 == pTestName2,		"> Second Name test failed.")
	
	CHECK((InvalidName == Cb::InvalidIdString) &&
		  (InvalidName2 == Cb::InvalidIdString),
		  "> Invalid name test failed.")
	
	Test.Clear();
	IdCounter.m_Count = 0;
	Test.ForEachId(IdCounter); // Should be 0
	
	Id7 = Test.RegisterId(pTestName3);	// should be 0
	
	CHECK(IdCounter.m_Count == 0,		"> Clear test failed.")
	CHECK(Id7 == 0,						"> Unlock after clear test failed.")
	
END_TEST_CASE_IMPLEMENTATION
